package com.suoyue.store.service.impl;

import com.suoyue.store.entity.Cart;
import com.suoyue.store.entity.CartObject;
import com.suoyue.store.mapper.CartMapper;
import com.suoyue.store.service.ex.ServiceException;
import com.suoyue.store.service.iCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartServiceImpl implements iCartService {
    @Autowired
    CartMapper cartMapper;

    @Override
    public List<CartObject> getCart(Integer uid) {

        List<CartObject> result=cartMapper.findCartByUid(uid);
        if (result.size()==0){
            throw new ServiceException("购物车为空");
        }
        return result;
    }

    @Override
    public void insertCart(Integer uid, Integer pid,Integer num) {

        Cart result=cartMapper.findCartByUidAndPid(uid,pid);
        if(result!=null){
            throw new ServiceException("购物车已存在该商品");
        }
        Integer rows=cartMapper.insertCart(uid,pid,num);
        if(rows!=1){
            throw new ServiceException("添加购物车失败");
        }



    }

    @Override
    public void addCartByCid(Integer cid) {
        Integer rows=cartMapper.addCartByCid(cid);
        if(rows!=1){
            throw new ServiceException("添加失败");
        }
    }

    @Override
    public void minusCartByCid(Integer cid) {
        Integer rows=cartMapper.minusCartByCid(cid);
        if(rows!=1){
            throw new ServiceException("减少失败");
        }
    }

    @Override
    public void deleteCartByCid(Integer cid) {
        Integer rows=cartMapper.deleteCartByCid(cid);
        if(rows!=1){
            throw new ServiceException("删除失败");
        }
    }

    @Override
    public void deleteAllCartByUid(Integer uid) {
        cartMapper.deleteAllCartByUid(uid);
    }


}
